//
//  ZhifuCell1.m
//  夺宝吧
//
//  Created by xiaocai on 15/11/27.
//  Copyright © 2015年 FangYuan. All rights reserved.
//

#import "ZhifuCell1.h"
#import "AFFNumericKeyboard.h"

@interface ZhifuCell1 ()<AFFNumericKeyboardDelegate,UITextFieldDelegate>


@end

@implementation ZhifuCell1

- (void)awakeFromNib {
    // Initialization code
//    [super awakeFromNib];
    [self setUpBorder];
    
    [self setUpTextField];
    
    self.otherMoneyField.delegate = self;
    
    [self fiftyBtnClick:self.fiftyBtn];
}
- (void)setUpTextField{
    AFFNumericKeyboard *keyboard = [[AFFNumericKeyboard alloc] initWithFrame:CGRectMake(0, WYMHEIGHT-216, WYMWITH, 216)];
    self.otherMoneyField.inputView = keyboard;
    keyboard.delegate = self;
}

#pragma mark - self.otherField 的代理方法
//0 1 2 ..按键的点击
- (void) numberKeyboardInput:(NSInteger) number{
    self.otherMoneyField.text = [self.otherMoneyField.text stringByAppendingString:[NSString stringWithFormat:@"%ld",number]];
}
//删除按键
- (void) numberKeyboardBackspace{
    
    if (self.otherMoneyField.text.length != 0){
        self.otherMoneyField.text = [self.otherMoneyField.text substringToIndex:self.otherMoneyField.text.length - 1];
    }
    
}
//改变键盘类型
- (void) changeKeyboardType{
    
}
- (void) doneBtnOnClick{
    [self.otherMoneyField resignFirstResponder];
}

#pragma mark -- otherMoneyField的代理方法用于border颜色的修改
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    self.selectedBtn.selected = NO;
    [self setBtnBorder:self.selectedBtn selected:self.selectedBtn.selected];
    self.selectedBtn = nil;
    
    UIColor *borderColor =  [UIColor redColor];
    textField.layer.borderColor = borderColor.CGColor;
    textField.layer.borderWidth = 1.0f;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self notifyDelegateResighResponder:textField];
}
#pragma mark -- 添加边框
//为6个按钮添加 黑色边框
- (void)setUpBorder{
    UIColor *borderColor =  [UIColor grayColor];
//    self.fiftyBtn.backgroundColor = [UIColor redColor];
    self.fiftyBtn.layer.borderColor = borderColor.CGColor;
    self.fiftyBtn.layer.borderWidth = 1.0f;
//    self.fiftyBtn.layer.cornerRadius = 4.0f;
    
    self.hundredBtn.layer.borderColor = borderColor.CGColor;
    self.hundredBtn.layer.borderWidth = 1.0f;
//    self.hundredBtn.layer.cornerRadius = 4.0f;
    
    self.twoHundredBtn.layer.borderColor = borderColor.CGColor;
    self.twoHundredBtn.layer.borderWidth = 1.0f;
//    self.twoHundredBtn.layer.cornerRadius = 4.0f;

    self.fiveHundredBtn.layer.borderColor = borderColor.CGColor;
    self.fiveHundredBtn.layer.borderWidth = 1.0f;
//    self.fiveHundredBtn.layer.cornerRadius = 4.0f;

    self.thousandsBtn.layer.borderColor = borderColor.CGColor;
    self.thousandsBtn.layer.borderWidth = 1.0f;
//    self.thousandsBtn.layer.cornerRadius = 4.0f;
    
    self.otherMoneyField.layer.borderColor = borderColor.CGColor;
    self.otherMoneyField.layer.borderWidth = 1.0f;
//    self.otherMoneyBtn.layer.cornerRadius = 4.0f;

}

//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
//
//    // Configure the view for the selected state
//}

- (IBAction)fiftyBtnClick:(UIButton *)sender {
    [self.otherMoneyField resignFirstResponder];
    [self notifyDelegateWithBtn:sender];
    
    if (self.selectedBtn != sender){
        
        self.selectedBtn.selected = NO;
        [self setBtnBorder:self.selectedBtn selected:self.selectedBtn.selected];
        
        sender.selected= YES;
        [self setBtnBorder:sender selected:sender.selected];
        self.selectedBtn = sender;
    }
    
    
//    UIColor *borderColor =  [UIColor redColor];
//    
//    sender.layer.borderColor = borderColor.CGColor;
//    sender.layer.borderWidth = 1.0f;
    
}

- (IBAction)hundredBtnClick:(UIButton *)sender {
    [self.otherMoneyField resignFirstResponder];
    [self notifyDelegateWithBtn:sender];
    
    if (self.selectedBtn != sender){
        
        self.selectedBtn.selected = NO;
        [self setBtnBorder:self.selectedBtn selected:self.selectedBtn.selected];
        
        sender.selected= YES;
        [self setBtnBorder:sender selected:sender.selected];
        self.selectedBtn = sender;
    }

}

- (IBAction)twoHundredBtnClick:(UIButton *)sender {
    [self.otherMoneyField resignFirstResponder];
    [self notifyDelegateWithBtn:sender];
    
    if (self.selectedBtn != sender){
        
        self.selectedBtn.selected = NO;
        [self setBtnBorder:self.selectedBtn selected:self.selectedBtn.selected];
        
        sender.selected= YES;
        [self setBtnBorder:sender selected:sender.selected];
        self.selectedBtn = sender;
    }

}

- (IBAction)fiveHundredBtnClick:(UIButton *)sender {
    [self.otherMoneyField resignFirstResponder];
    [self notifyDelegateWithBtn:sender];
    
    if (self.selectedBtn != sender){
        
        self.selectedBtn.selected = NO;
        [self setBtnBorder:self.selectedBtn selected:self.selectedBtn.selected];
        
        sender.selected= YES;
        [self setBtnBorder:sender selected:sender.selected];
        self.selectedBtn = sender;
    }

}

- (IBAction)thousandBtnClick:(UIButton *)sender {
    [self.otherMoneyField resignFirstResponder];
    [self notifyDelegateWithBtn:sender];
    
    if (self.selectedBtn != sender){
        
        self.selectedBtn.selected = NO;
        [self setBtnBorder:self.selectedBtn selected:self.selectedBtn.selected];
        
        sender.selected= YES;
        [self setBtnBorder:sender selected:sender.selected];
        self.selectedBtn = sender;
    }

}

- (void)setBtnBorder:(UIButton*)btn selected:(BOOL)selected{
    
    UIColor *borderColor =  [UIColor grayColor];
    self.otherMoneyField.layer.borderColor = borderColor.CGColor;
    self.otherMoneyField.layer.borderWidth = 1.0f;
    if (selected){
        UIColor *borderColor =  [UIColor redColor];
        btn.layer.borderColor = borderColor.CGColor;
        btn.layer.borderWidth = 1.0f;
    }else{
        UIColor *borderColor =  [UIColor grayColor];
        btn.layer.borderColor = borderColor.CGColor;
        btn.layer.borderWidth = 1.0f;
    }
}
//通知代理按钮点击了
- (void)notifyDelegateWithBtn:(UIButton*)sender{
    
    if ([self.delegate respondsToSelector:@selector(ZhifuCell1:button:)]){
        [self.delegate ZhifuCell1:self button:sender];
    }
    
}
//通知代理输入框没有了输入
- (void)notifyDelegateResighResponder:(UITextField*)textField{
    if(textField.text.integerValue > CHONGZHI_LIMIT){
        [WYPublic showErrorWithTitle:@"" Msg:@"充值额度不能超过1000 !"];
        textField.text = [NSString stringWithFormat:@"%d",CHONGZHI_LIMIT];
        
    }
    if ([self.delegate respondsToSelector:@selector(ZhifuCell1:otherMoneyFieldResignFirstResponder:)]){
        [self.delegate ZhifuCell1:self otherMoneyFieldResignFirstResponder:textField];
    }
}

@end
